<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>service-details</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

		<link rel="stylesheet" href="../../../../css/mui.min.css">
		<link rel="stylesheet" href="../../../../css/global.css">
		<link rel="stylesheet" href="../../../../css/style.css">
		<link rel="stylesheet" href="../../../../css/mui.picker.css">
		<link rel="stylesheet" href="../../../../css/mui.poppicker.css">
		<style type="text/css">
			.mui-btn-block{
				border: 0;
				font-size: 16px;
				color: #a9a9a9;
				text-align: left;
				line-height: 20px;
				white-space: normal;
				margin-bottom: 0;
				background-color: initial;
			}
			.details_con .clum {
				height: auto;				
			}
			.mui-pciker-list li {
				width: 99%;
			    display: block;
			    line-height: 18px;
			    white-space: normal;
				font-size: 14px;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 id="title" class="mui-title">Hot Account Report</h1>
		</header>
		<div class="mui-content">
			<input type="hidden" id="helpId" />
			<div class="details_con">
				<div class="clum">
					<div class="name">Hot Account Type<em class="t_red">*</em></div>
					<div class="txt">
						<input type="hidden" id="hotAccountType"/>
						<button id='hotAccountTypeName' class="mui-btn mui-btn-block mui-btn-select" type='button' mulitline="true">请选择</button>
					</div>
				</div>
				<div class="clum">
					<span class="name">Fail Type<em class="t_red">*</em></span>
					<span class="txt">
						<input type="hidden" id="failType"/>
						<button id='failTypeName' class="mui-btn mui-btn-block mui-btn-select" type='button'>请选择</button>
					</span>
				</div>
				<div class="clum">
					<div class="name">Severity Level<em class="t_red">*</em></div>
					<div class="txt">
						<input type="hidden" id="severityLevel"/>
						<button id='severityLevelName' class="mui-btn mui-btn-block mui-btn-select" type='button' mulitline="true">请选择</button>
					</div>
				</div>
				<div class="clum">
					<span class="name">Instrument Status<em class="t_red">*</em></span>
					<span class="txt">
						<button id='instrunmentStatus' class="mui-btn mui-btn-block mui-btn-select" data-val="" type='button'>请选择</button>
					</span>
				</div>
				<div class="clum">
					<span class="name">Start Date</span>
					<button id='startDate'  type="button" class="selectDate">请选择时间</button>
				</div>
				<div class="clum">
					<span class="name">End Date</span>
					<button id='endDate'  type="button" class="selectDate">请选择时间</button>
				</div>
				<div class="clum nobor autoHeight">
					<span class="name">Problem<em class="t_red">*</em></span>
					<textarea maxlength="1000" class="textarea" id="problem" style="margin-bottom: 0;"></textarea> 
				</div>
				<div class="clum nobor autoHeight">
					<span class="name">Work done(incldue Date)<em class="t_red">*</em></span>
					<textarea maxlength="1000" class="textarea" id="workDone" style="margin-bottom: 0;"></textarea> 
				</div>
				<div class="clum">
					<span class="name">Equipment Model</span>
					<span class="txt">
						<input type="hidden" id="equipmentModeValue" />
						<button id='equipmentMode' class="mui-btn mui-btn-block mui-btn-select" type='button'>请选择</button>
					</span>
				</div>
				<div class="clum">
					<span class="name">Serial No</span>
					<input type="text" maxlength="50" id="serialNo" class="d_ipt" placeholder="请输入Serial No" style="padding: 0;"/>
				</div>
				<div class="my-content-cell"  id="partsList">
					<div class="inline-block">
						<em>备件列表</em>
						<span class="mui-icon mui-icon-arrowright arrow-right-position"></span>
					</div>
				</div>
				<!-- <div class="my-content-cell" id="addParts">
					<div class="inline-block">
						<em>添加备件</em>
						<span class="mui-icon mui-icon-arrowright arrow-right-position"></span>
					</div>
				</div> -->
				
			</div>
			<div class="btn_box">
				<button class="fl"  id="saveBtn">保存草稿</button>
				<button class="fr red" id="submitBtn">正式提交</button>
			</div>
			<div style="height: 50px;"></div>
		</div>

		<!-- js代码 start -->

		<!-- 基础js start -->
		<script src="../../../../js/mui.min.js"></script>
		<script src="../../../../js/utils/h.js"></script>
		<script src="../../../../js/constant.js"></script>
		<script src="../../../../js/utils/base64.js"></script>
		<script src="../../../../js/common.js"></script>
		<script src="../../../../js/utils/md5.js"></script>
		<script src="../../../../js/utils/sortMap.js"></script>
		<script src="../../../../js/utils/http.js"></script>
		<script src="../../../../js/app.js"></script>
		<!-- 基础js end -->
		<script src="../../../../js/utils/rolldate.min.js"></script>
		<script src="../../../../js/mui.picker.js"></script>
		<script src="../../../../js/mui.poppicker.js"></script>
		<script type="text/javascript" charset="utf-8">
			var serviceNo
			var planId
			var noticeId
			var id
			var problem
			var divBu
			(function($, doc) {
				$.init();
		
				$.plusReady(function() {
					initClick()
					initDateTimeControls()
					 
					var self = plus.webview.currentWebview();
					serviceNo = self.serviceNo
					planId = self.planId
					noticeId = self.noticeId
					problem = self.problem
					divBu = self.divBu
					var helpId = new Date().UUID(self.serviceNo);
					h('#helpId').val(helpId);
					h('#problem').val(problem);
					//查询最新的服务报告，带回开始结束时间
					getReport(serviceNo, planId, noticeId);
					//备件列表
					h('#partsList').tap(function() {
						app.jumpTo('/view/service/report/errorhot/sparePartsList.html','view/service/report/errorhot/sparePartsList.html',{
							serviceNo:serviceNo,
							helpId:h('#helpId').val(),
							portType: 4
						})
					})
					//添加备件
					h('#addParts').tap(function() {
						app.jumpTo('/view/service/report/errorhot/sparePartsAdd.html','view/service/report/errorhot/sparePartsAdd.html',{
							serviceNo:serviceNo,
							helpId:h('#helpId').val(),
							portType: 4
						})
					})
					// 保存按钮触发事件
					h('#saveBtn').tap(function() {
						h(this).attr('disabled','disabled');
						app.hideSoftKeybord()
						saveHotData('正在保存...', function(data) {
							app.closeWaiting()
							if (data.code === 200) {
								id = data.data.id
								app.toast("保存成功")
								// 查找需要跳转的页面，并指定刷新的方法
								var erroOrHot = plus.webview.getWebviewById('view/service/report/errorhot/erroOrHot.html');
								if (erroOrHot) {
									erroOrHot.close()
								}
								mui.back();
							} else {
								h('#saveBtn').removeAttr('disabled');
								h('#submitBtn').removeAttr('disabled');
								app.toast(data.msg)
							}
						})
					})
					
					// 提交
					h('#submitBtn').tap(function() {
						h(this).attr('disabled','disabled');
						app.hideSoftKeybord()
						saveHotData('正在提交...', function(data) {
							console.log(JSON.stringify(data))
							if (data.code === 200) {
								id = data.data.id
								hotAccountTypeSubmit()
							} else {
								h('#saveBtn').removeAttr('disabled');
								h('#submitBtn').removeAttr('disabled');
								app.closeWaiting()
								app.toast(data.msg)
							}
						})
					})
				});
			}(mui, document));
			
			//查询最新的服务报告，带回开始结束时间
			function getReport(serviceNo, planId, noticeId){
				app.showWaiting('加载中...')
				ajaxGet(BASE_URL + 'cc/report/info/selectReportStartEndTime', {
					serviceNo: serviceNo,
					enginnerId: app.getUser().enginnerId
				}, function(data){
					app.closeWaiting()
					if (data.code === 200) {
						var startTime = data.data && data.data.startTime ? emptyStr(data.data.startTime) : '';
						var endTime = data.data && data.data.startTime ? emptyStr(data.data.endTime) : '';
						if (startTime) {
							h("#startDate").html(startTime.substring(0, startTime.indexOf(" ")).trim());
						}
						if (endTime) {
							h("#endDate").html(endTime.substring(0, endTime.indexOf(" ")).trim());
						}
					} else {
						//app.toast(data.msg)
					}
				}, function(errorMsg){
					app.closeWaiting()
					app.toast(errorMsg)
				});
			}
			
			function hotAccountTypeSubmit() {
				var enginnerId = app.getUser().enginnerId
				ajaxGet(BASE_URL + 'cc/hot/account/report/hoatAccountReportSubmit', {
					id: id,
					enginnerId: enginnerId
				}, function(data){
					app.closeWaiting()
					if (data.code === 200) {
						app.toast("提交成功")
						// 查找需要跳转的页面，并指定刷新的方法
						var erroOrHot = plus.webview.getWebviewById('view/service/report/errorhot/erroOrHot.html');
						if (erroOrHot) {
							erroOrHot.close()
						}
						mui.back();
					} else {
						h('#saveBtn').removeAttr('disabled');
						h('#submitBtn').removeAttr('disabled');
						app.toast(data.msg)
					}
					
					
				}, function(errorMsg){
					h('#saveBtn').removeAttr('disabled');
					h('#submitBtn').removeAttr('disabled');
					app.closeWaiting()
					app.toast(errorMsg)
				});
			}
			
			function saveHotData(tips, callBack) {
				console.log(h('#problem').val().length)
				var hotAccountTypeName = h('#hotAccountTypeName').html()
				var hotAccountType = h('#hotAccountType').val()
				if (!hotAccountTypeName || hotAccountTypeName == "请选择") {
					h('#saveBtn').removeAttr('disabled');
					h('#submitBtn').removeAttr('disabled');
					app.toast('请选择Hot Account Type')
					return
				}
				
				var failTypeName = h('#failTypeName').html()
				var failType = h('#failType').val()
				if (!failTypeName || failTypeName == "请选择") {
					h('#saveBtn').removeAttr('disabled');
					h('#submitBtn').removeAttr('disabled');
					app.toast('请选择Fail Type')
					return
				}
				
				var severityLevelName = h('#severityLevelName').html()
				var severityLevel = h('#severityLevel').val()
				if (!severityLevelName || severityLevelName == "请选择") {
					h('#saveBtn').removeAttr('disabled');
					h('#submitBtn').removeAttr('disabled');
					app.toast('请选择Severity Level')
					return
				}
				
				var instrunmentStatus = h('#instrunmentStatus').attr('data-val')
				if (!instrunmentStatus || instrunmentStatus == "请选择") {
					h('#saveBtn').removeAttr('disabled');
					h('#submitBtn').removeAttr('disabled');
					app.toast('请选择Instrument Status')
					return
				}
				
				var equipmentMode = h('#equipmentModeValue').val();
				if (!equipmentMode || equipmentMode == "请选择") {
					equipmentMode = "";
				}
				
				var startDate = h('#startDate').html()
				if (!startDate || startDate == "请选择时间") {
					startDate = "";
				}
				
				var endDate = h('#endDate').html();
				if (!endDate || endDate == "请选择时间") {
					endDate = "";
				}
				
				if (startDate && endDate) {
					if (stringYYYYMMDDHHMMToDate(startDate+' 00:00').getTime() > stringYYYYMMDDHHMMToDate(endDate+' 00:00').getTime()) {
						h('#saveBtn').removeAttr('disabled');
						h('#submitBtn').removeAttr('disabled');
						app.toast('Start Date不能大于End Date')
						return false
					}
				}
				
				
				var problem = h('#problem').val()
				if (!problem) {
					h('#saveBtn').removeAttr('disabled');
					h('#submitBtn').removeAttr('disabled');
					app.toast('请输入Problem')
					return
				}
				var workDone = h('#workDone').val()
				if (!workDone) {
					h('#saveBtn').removeAttr('disabled');
					h('#submitBtn').removeAttr('disabled');
					app.toast('请输入Work done(incldue Date)')
					return
				}
				
				var enginnerId = app.getUser().enginnerId
				var fseName = app.getUser().name;
				var params = {
					hotAccountType: hotAccountType,
					hotAccountTypeName: hotAccountTypeName,
					failType: failType,
					failTypeName: failTypeName,
					severityLevel: severityLevel,
					severityLevelName: severityLevelName,
					instrunmentStatus: instrunmentStatus,
					startDate: startDate == "" ? "" : startDate+ ' 00:00:00',
					endDate: endDate == "" ? "" : endDate+ ' 00:00:00',
					problem: h('#problem').val(),
					workDone: h('#workDone').val(),
					equipmentMode : equipmentMode,
					serialNo : h('#serialNo').val(),
					planId : planId,
					noticeId : noticeId,
					enginnerId : enginnerId,
					fseName: fseName,
					serviceNo : serviceNo,
					helpId: h('#helpId').val()
				}
				if(typeof(params) == "object"){
					var code = String.fromCharCode(8198)
					Object.keys(params).forEach(function (key) {
						if (typeof(params[key]) == 'string'){
							var str = params[key].split(code).join(' ')
							params[key]=str
							str = null;
						}
						// params[key].split(code).join('!!!!!')

					})
					console.debug(params)
				}
				
				if (id) {
					params.id = id;
				}
				
				if (!app.getNetworkState()) {
					h('#saveBtn').removeAttr('disabled');
					h('#submitBtn').removeAttr('disabled');
					app.closeWaiting()
					app.toast("无网络")
					return
				}
				app.showWaiting(tips)
				ajaxPost(BASE_URL + 'cc/hot/account/report/addHoatAccountReport', params, function(data) {
					callBack(data)
				}, function(err) {
					h('#saveBtn').removeAttr('disabled');
					h('#submitBtn').removeAttr('disabled');
					app.closeWaiting()
					app.toast(err)
				})
			}
			
			function initClick() {
				// hotAccountType
				h('#hotAccountTypeName').tap(function() {
					app.hideSoftKeybord()
					app.showWaiting()
					var userPicker = new mui.PopPicker();
					ajaxGetJson(BASE_URL + 'cc/dict/selectHotAccountType', {
						engineerId: app.getUser().enginnerId
					}, function(data) {
						app.closeWaiting()
						console.log(JSON.stringify(data))
						if (!data || data.length <= 0) {
							app.toast("无Hot Account Type数据")
							return
						}
						userPicker.setData(data);
						userPicker.show(function(items) {
							h('#hotAccountTypeName').html(items[0].text)
							h('#hotAccountType').val(items[0].value)
							userPicker.dispose()
						});
					});
				})
				
				// failType
				h('#failTypeName').tap(function() {
					app.hideSoftKeybord()
					app.showWaiting()
					var userPicker = new mui.PopPicker();
					ajaxGetJson(BASE_URL + 'cc/dict/selectCCFailtype', {
						engineerId: app.getUser().enginnerId
					}, function(data) {
						app.closeWaiting()
						console.log(JSON.stringify(data))
						if (!data || data.length <= 0) {
							app.toast("无Fail Type数据")
							return
						}
						
						var typeData 
						if(divBu.indexOf('CMD')!= -1){
							typeData = data;
						} else {
							typeData = data.splice(0,5);
						}
						
						userPicker.setData(typeData);
						userPicker.show(function(items) {
							h('#failTypeName').html(items[0].text)
							h('#failType').val(items[0].value)
							userPicker.dispose()
						});
					});
				})
				
				
				// severityLevel
				h('#severityLevelName').tap(function() {
					app.hideSoftKeybord()
					app.showWaiting()
					var userPicker = new mui.PopPicker();
					ajaxGetJson(BASE_URL + 'cc/dict/selectCCHTSeverity', {
						engineerId: app.getUser().enginnerId
					}, function(data) {
						app.closeWaiting()
						console.log(JSON.stringify(data))
						if (!data || data.length <= 0) {
							app.toast("无Severity Level数据")
							return
						}
						userPicker.setData(data);
						userPicker.show(function(items) {
							h('#severityLevelName').html(items[0].text)
							h('#severityLevel').val(items[0].value)
							userPicker.dispose()
						});
					});
				})
				
				// instrunmentStatus
				h('#instrunmentStatus').tap(function() {
					app.hideSoftKeybord()
					app.showWaiting()
					var userPicker = new mui.PopPicker();
					ajaxGetJson(BASE_URL + 'cc/dict/selectCCInstrumentStatus', {
						engineerId: app.getUser().enginnerId
					}, function(data) {
						app.closeWaiting()
						console.log(JSON.stringify(data))
						if (!data || data.length <= 0) {
							app.toast("无Instrunment Status数据")
							return
						}
						userPicker.setData(data);
						userPicker.show(function(items) {
							h('#instrunmentStatus').html(items[0].text)
							h('#instrunmentStatus').attr('data-val',items[0].value)
							console.log(h('#instrunmentStatus').attr('data-val'))
							userPicker.dispose()
						});
					});
				})
				
				// equipmentMode
				h('#equipmentMode').tap(function() {
					app.hideSoftKeybord()
					app.showWaiting()
					var userPicker = new mui.PopPicker();
					ajaxGetJson(BASE_URL + 'cc/dict/selectCCEquipmentModel', {
						engineerId: app.getUser().enginnerId
					}, function(data) {
						app.closeWaiting()
						console.log(JSON.stringify(data))
						if (!data || data.length <= 0) {
							app.toast("无Equipment Mode数据")
							return
						}
						userPicker.setData(data);
						userPicker.show(function(items) {
							h('#equipmentMode').html(items[0].text)
							h('#equipmentModeValue').val(items[0].value)
							userPicker.dispose()
						});
					});
				})
			}
			
			/**
			 * 初始化日期时间控件
			 */
			function initDateTimeControls() {
				// Error发生时间
				new Rolldate({
					el: '#startDate',
					format: 'YYYY-MM-DD',
					beginYear: 2015,
					endYear: 2050,
					trigger: 'click',
					lang: {
						title: '请选择发生日期',
					},
					confirm: function(date) {
						
					}
				})
				
				//跟踪日期
				new Rolldate({
					el: '#endDate',
					format: 'YYYY-MM-DD',
					beginYear: 2015,
					endYear: 2050,
					trigger: 'click',
					lang: {
						title: '请选择跟踪日期',
					},
					confirm: function(date) {
						
					}
				})
			}
			
		</script>
		<!-- js代码 end -->

	</body>

</html>
